<?php
/**
 * Created by 绵阳人维网络科技有限公司.
 * User: 罗东华
 * Date: 2016/3/17
 * Time: 10:01
 */
namespace Admin\Controller;
class SitesController extends AdminBase{

    public function lists(){

        if($_GET['keywords']){
            $keywords=I('get.keywords');
             $where['site_name']=array('like',"%$keywords%");
             $where['site_company']=array('like',"%$keywords%");
             $where['_logic']='or';
        }else{
            $where=true;
        }
        $this->assign('count',M('Sites')->count());
        $this->assign('keywords',$keywords);
        $lists=$this->Pages('Sites',$where);
        $this->assign('lists',$lists);
        $this->display();
    }
    public function add(){
        if(IS_POST){

            $this->ThisReturn(D('Sites')->db_add());
        }
        $this->display();
    }

    public function name_check(){

        if(IS_AJAX){
            if(I('get.id')){$where['site_id']=array('neq',I('get.id'));}
            $this->ajaxReturn(field_check('Sites',$where));
        }

    }
    /*编辑站点*/
    public function edit(){

        $site_id=I('get.id',0,'int');
        $where['site_id']=$site_id;

        if(IS_POST){

            $this->ThisReturn(D('Sites')->db_add('','','save',$where));

        }


        $this->assign(M('Sites')->where($where)->find());
        $this->display();
    }
    /*删除站点*/
    public function del(){
        if(IS_AJAX){

            $this->ajaxReturn(D('Sites')->del());
        }


    }
    public function domain(){
        $site_id=I('get.sid');
        $where['site_id']=$site_id;
        $site_name=M('Sites')->where($where)->getField('site_name');

        $domains=M('domain')->where($where)->select();
        $this->assign('site_name',$site_name);
        $this->assign('site_id',$site_id);
        $this->assign('domains',$domains);
        $this->display();
    }
    public function domain_add(){
        $sid=I('get.sid');
        if(!$sid){return false;}
        if(IS_POST){
            $Domain=M('Domain');
            $Domain->create();
            $id=$Domain->add();
           if($id>0){
               $res=true;
           }else{
               $res=false;
           }
            $this->ThisReturn($res);
        }
        $this->assign('sid',$sid);
        $this->display();
    }
    public function domain_check(){
        if(IS_AJAX){
            $where['id']=array('neq',I('get.id',0,'int'));
            $this->ajaxReturn(field_check('domain',$where));
        }

    }
    public function domain_save(){
        $sid=$where['site_id']=I('get.sid',0,'int');
        $id=$where['id']=I('get.id',0,'int');
        if(!$sid || !$id){return false;}
        if(IS_POST){
            $Domain=M('Domain');
            $Domain->create();
            $Domain->id=$id;
            $id=$Domain->save();
            if($id>0){
                $res=true;
            }else{
                $res=false;
            }
            $this->ThisReturn($res);
        }
        $this->assign(M('domain')->where($where)->find());
        $this->assign('sid',$sid);
        $this->display();

    }
    public function domain_del(){

        if(IS_AJAX){

            $where['id']=$idx=I('get.id');
            if(!$idx){return false;}
            $id=M('domain')->where($where)->delete();
            if($id>0){
                $data['info']='删除成功';
                $data['status']='y';
            }else{
                $data['info']='删除失败';

            }
            $this->ajaxReturn($data);
        }

    }
    public function domain_status(){
    if(IS_AJAX){
        $where['id']=$id=I('get.id');
        if(!$id){return false;}
        $status=M('domain')->where($where)->getField('status');
        if($status==0){
            $data['status']=1;

        }else{
            $data['status']=0;

        }
        $res=M('domain')->where($where)->save($data);

        if($res>0){
            $data['info']='更新成功';
            $data['status']='y';
        }else{
            $data['info']='更新失败';

        }
        $this->ajaxReturn($data);


    }

    }
    public function domain_zc(){

        if(IS_AJAX){

            $where['site_id']=I('get.sid');
            M('domain')->where($where)->save(array('ranking'=>0));
            $where['id']=$id=I('get.id');
            $res=M('domain')->where($where)->save(array('ranking'=>1));

            if($res>0){
                $data['info']='更新成功';
                $data['status']='y';
            }else{
                $data['info']='更新失败';

            }
            $this->ajaxReturn($data);


        }


    }
}